<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Equity extends Stourweb_Controller
{
    public function action_index()
    {
        $list = Model_Equity::equity_list();

        $this->assign('list', $list);
        $this->display('stourtravel/equity/index');
    }

    public function action_dialog_add_equity()
    {
        $list = Model_Equity::equity_list();

        $this->assign('list', $list);
        $this->display('stourtravel/equity/dialog_add_equity');
    }

    public function action_ajax_save_equity()
    {
        $id = Arr::get($_POST, 'id');
        if(count($id)>0){
            foreach ($id as $v){
                $info = Model_Equity::get_equity_icon_info($v);
                unset($info['id']);
                DB::insert('member_equity', array_keys($info))->values(array_values($info))->execute();
            }
        }

        echo json_encode(array('status' => true));
    }

    public function action_ajax_update_equity()
    {
        $id = Arr::get($_POST, 'id');
        $icon = Arr::get($_POST, 'icon');
        $title = Arr::get($_POST, 'title');
        $ishidden = Arr::get($_POST, 'ishidden');

        if($icon){
            DB::update('member_equity')->set(['icon'=>$icon])->where('id','=',$id)->execute();
        }
        if($title){
            DB::update('member_equity')->set(['title'=>$title])->where('id','=',$id)->execute();
        }
        if($ishidden || $ishidden==='0'){
            $ishidden = $ishidden?0:1;
            DB::update('member_equity')->set(['ishidden'=>$ishidden])->where('id','=',$id)->execute();
        }

        echo json_encode(array('status' => true));
    }

    public function action_ajax_delete_equity()
    {
        $id = Arr::get($_POST, 'id');
        $res = DB::delete('member_equity')->where('id','=',$id)->execute();

        echo json_encode(array('status' => $res?true:false));
    }

    public function action_ajax_equity_sort()
    {
        $id = Arr::get($_POST, 'id');
        foreach ($id as $k=>$v){
            DB::update('member_equity')->set(array('sort'=>$k+1))->where('id','=',$v)->execute();
        }
    }
}